package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {

    @Autowired
    private EmpService empService;

    @Lazy
    @Autowired
    private StudentService studentService;

    /**
     * 班级分页查询
     *
     * @param name
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult<Clazz> pageClazz(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        LambdaQueryWrapper<Clazz> wrapper = Wrappers.<Clazz>lambdaQuery()
                .like(!StringUtils.isEmpty(name), Clazz::getName, name)
                .between(begin != null && end != null, Clazz::getEndDate, begin, end)
                .orderByDesc(Clazz::getUpdateTime);


        // 创建分页参数对象
        IPage<Clazz> ipage = new Page<>(page, pageSize);

        // 返回分页结果
        IPage<Clazz> clazzIPageResult = this.page(ipage, wrapper);
        List<Clazz> clazzIPageResultRecords = clazzIPageResult.getRecords();
        // 查询当前页班主任id
        List<Integer> masterIdList = clazzIPageResultRecords.stream().map(clazz -> clazz.getMasterId()).distinct().toList();
        // 查询对应班主任对象
        List<Emp> empList = empService.list(Wrappers.<Emp>lambdaQuery().in(Emp::getId, masterIdList));
        LocalDate now = LocalDate.now();
        // 统一赋值
        clazzIPageResultRecords.forEach(clazz -> {
            if (clazz.getBeginDate().isAfter(now)) {
                clazz.setStatus("未开课");
            } else if (clazz.getEndDate().isBefore(now)) {
                clazz.setStatus("已结课");
            } else {
                clazz.setStatus("上课中");
            }

            empList.stream()
                    .filter(emp -> emp.getId().equals(clazz.getMasterId()))
                    .findFirst()
                    .ifPresent(emp -> clazz.setMasterName(emp.getName()));
        });


        return new PageResult<>(clazzIPageResult.getTotal(), clazzIPageResultRecords);
    }

    /**
     * 新增班级
     *
     * @param clazz
     */
    @Override
    public void saveClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        this.save(clazz);
    }

    /**
     * 根据id查询班级
     *
     * @param id
     * @return
     */
    @Override
    public Clazz getClazzById(Integer id) {
        return this.getById(id);
    }

    /**
     * 修改班级
     *
     * @param clazz
     */
    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        this.updateById(clazz);
    }

    /**
     * 删除班级
     *
     * @param id
     */
    @Override
    public void delectClazz(Integer id) {
        // 班级下面有学生不能删除

        List<Student> studentList = studentService.list(Wrappers.<Student>lambdaQuery().eq(Student::getClazzId, id));
        if (!CollectionUtils.isEmpty(studentList)) {
            throw new RuntimeException("有学生不能删除啊");
        }
       this.removeById(id);
    }

    /**
     * 查询全部班级
     */
    @Override
    public void listClazz() {
        this.list();
    }
}
